import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:qyou_app/common/base.dart';

import 'channel/channel.dart';
import 'dynamic/dynamic.dart';
import 'home/home.dart';
import 'my/my.dart';
import 'shop/shop.dart';

class IndexPage extends StatefulWidget{

  @override
  State<StatefulWidget> createState() => _IndexPageState();
  
}

class _IndexPageState extends State<IndexPage> with Base{
  //底部导航
  final _bottomNavList = ["首页","频道","动态","商城","我的"];
  DateTime _lastPressedAt; //上次点击时间
  int _selectedIndex = 0;
  ScrollController _scrollController = ScrollController(); // 首页整体滚动控制器
  PageController _pageController = PageController();

  //加载底部导航栏图片
  Widget _icon(path) {
    return Padding(
        padding: EdgeInsets.only(bottom: dp(4)),
        child: Image.asset(
          path,
          width: dp(25),
          height: dp(25),
          repeat: ImageRepeat.noRepeat,
          fit: BoxFit.contain,
          alignment: Alignment.center,
        ));
  }

  //点击底部导航栏选项卡
  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
    //页面跳转
    _pageController.jumpToPage(index);
  }

  //回到顶部
  void _toTop() {
    _scrollController.animateTo(0.0,
        duration: Duration(milliseconds: 300), curve: Curves.easeInOut);
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  //底部导航选项卡对应页面
  Widget _currentPage(){
    var _pages = [
      HomePage(_scrollController),
      ChannelPage(),
      DynamicPage(),
      ShopPage(_scrollController),
      MyPage(),
    ];

    return PageView.builder(
        physics: NeverScrollableScrollPhysics(),
        controller: _pageController,
        itemCount: _pages.length,
        itemBuilder: (context,index)=>_pages[index]
    );
  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
        designSize: Base.size,
        builder: () {
          return WillPopScope(
            //防止误触退出app
            onWillPop: () async {
              //两次点击间隔超过1秒则重新计时
              if (_lastPressedAt == null || DateTime.now().difference(_lastPressedAt) >
                  Duration(seconds: 1)) {
                _lastPressedAt = DateTime.now();
                Fluttertoast.showToast(msg: '再按一次退出酷油');
                return false;
              }
              return true;
            },
            child: Scaffold(
              body: _currentPage(),
              //底部导航
              bottomNavigationBar: BottomNavigationBar(
                backgroundColor: Colors.white,
                currentIndex: _selectedIndex,
                type: BottomNavigationBarType.fixed,
                selectedItemColor: Base.defaultColor,
                unselectedItemColor: Color(0xff333333),
                onTap: _onItemTapped,
                items: <BottomNavigationBarItem>[
                  BottomNavigationBarItem(
                    label: _bottomNavList[0],
                    icon: _selectedIndex == 0
                        ? _icon("assets/nav/home-12.png")
                        : _icon("assets/nav/home-11.png"),
                  ),
                  BottomNavigationBarItem(
                    label: _bottomNavList[1],
                    icon: _selectedIndex == 1
                        ? _icon("assets/nav/channel-22.png")
                        : _icon("assets/nav/channel-21.png"),
                  ),
                  BottomNavigationBarItem(
                    label: _bottomNavList[2],
                    icon: _selectedIndex == 2
                        ? _icon("assets/nav/dynamic-32.png")
                        : _icon("assets/nav/dynamic-31.png"),
                  ),
                  BottomNavigationBarItem(
                    label: _bottomNavList[3],
                    icon: _selectedIndex == 3
                        ? _icon("assets/nav/shop-42.png")
                        : _icon("assets/nav/shop-41.png"),
                  ),
                  BottomNavigationBarItem(
                    label: _bottomNavList[4],
                    icon: _selectedIndex == 4
                        ? _icon("assets/nav/account-52.png")
                        : _icon("assets/nav/account-51.png"),
                  ),
                ],
              ),
              //回到顶部悬浮按钮
              floatingActionButton: _selectedIndex != 0
                  ? null
                  : FloatingActionButton(
                  onPressed: _toTop,
                  foregroundColor: Base.defaultColor,
                  backgroundColor: Colors.white,
                  tooltip: '起飞',
                  child: Image.asset(
                    'assets/common/top.png',
                    width: dp(32),
                    height: dp(32),
                    fit: BoxFit.contain,
                  )),
              resizeToAvoidBottomInset: false, //是否重新布局，避免底部被遮盖
            ),
          );
        });
  }
  
}